import store from "@/store/index";
import { Message } from "element-ui";
// 必须包含列出的所有权限，元素才显示
export const hasPermission = {
  install(Vue, options) {
    Vue.directive("has", {
      inserted(el, binding, vnode) {
        let perms = binding.expression;
        let hasPermission = false;
        const btnPermission = store.state.user.currentPagePremission || [];
        btnPermission.forEach((element) => {
          if (perms == `'${element.permission}'`) {
            hasPermission = true;
          }
        });

        if (!hasPermission) {
          vnode.componentInstance.$off("click");
          el.onclick = () => {
            Message.error("您暂无操作权限，如需开通请联系管理员");
          };
        }
        //默认都有权限,但是只要其中有一个权限没有，则元素不会显示
      },
    });
  },
};
